<template>
	<view class="container">
		<view class="cut"></view>
		<view class="company">
			<image src="https://inclusivecommunity.oss-cn-chengdu.aliyuncs.com/attach/2020-09/163a120200908162818478.png" mode=""></image>
			<view class="info">
				<text class="a clamp">{{companyInfo.buyer_name}}</text>
				<text class="b">平均{{companyInfo.effective_hour}}小时到账</text>
			</view>
		</view>
		<view class="cut"></view>
		<view class="mbody">
			<view class="titlem">收购{{shell.name}}数量</view>
			<view class="titleDteals">
				<input class="myNumber" type="number" v-model="number" placeholder-style="font-weight:normal;font-size:30rpx"
				 placeholder="请输入收购数量" @input="change()" />
				<view class="qbText" @tap="allSell">
					<text>全部收购</text>
				</view>
			</view>
			<view class="notice">可收购{{shell.name}}数量{{shell.diesVenit}},最小递增单位10个贝壳。</view>
		</view>
		<view class="btn" @tap="submit">提交申请</view>
		<!-- 协议弹窗 -->
		<uni-popup ref="popup" type="bottom">
			<view class="ruleBox">
				<image class="close" @tap="close" src="../../../static/images/center/close.png" mode=""></image>
				<view class="content">
					<rich-text :nodes="detailObj"></rich-text>
				</view>
				<view class="btnBox">
					<view class="rule" @tap="ruleCheck">
						<image v-if="ruleChecked" src="../../../static/images/public/select.png" mode=""></image>
						<view class="gray" v-else></view>
						<view class="text"><text>阅读完并同意</text><text>《公司服务协议》</text></view>
					</view>
					<view class="btn" @tap="agree">同意</view>
				</view>
			</view>
		</uni-popup>
		<z-modal ref="zmodal" content="公司确认收购后将发送到您的账号,请您注意查收!" cancelShow="true" cancelColor="#4BA46E" cancelText="收购记录" confirmText="确认" @confirm="confirm"></z-modal>
	</view>
</template>

<script>
	import {
		buyerBuypact,
		shellrecycleApply
	} from '@/api/shell.js'
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import utils from "@/common/utils.js"
	export default {
		components:{
			uniPopup
		},
		data() {
			return {
				ruleChecked: true, //置换公司服务协议同意切换
				number: "",//置换数量
				companyInfo: {},//公司信息
				shell: {},//贝壳数据
				detailObj: "",//置换公司服务协议
			}
		},
		onLoad(options) {
			this.shell = JSON.parse(decodeURIComponent(options.shell))
			this.companyInfo = JSON.parse(decodeURIComponent(options.company))
			buyerBuypact(this.companyInfo.buyer_id).then(res => {
				this.detailObj = res.msg;
			})
		},
		methods: {
			// 数量监听
			change() {
				this.$nextTick(function() {
					let shellNum = Number(this.shell.diesVenit)
					this.number = utils.verifyNum(this.number)
					let number = Number(this.number)
					if (number > shellNum) {
						uni.showToast({
							icon: "none",
							title: "不能超过可收购数量"
						})
						this.number = shellNum
					}
				})

			},
			// 全部收购
			allSell() {
				this.number = parseInt(Number(this.shell.diesVenit)/10)*10
			},
			//切换服务协议同意
			ruleCheck() {
				this.ruleChecked = !this.ruleChecked
			},
			submit(){
				if (!this.number||this.number == 0) {
					return uni.showToast({
						icon:'none',
						title:"请输入收购数量"
					})
				}
				if (Number(this.number)%10 != 0) {
					return uni.showToast({
						icon:'none',
						title:"收购数量最小单位为10,请重新输入收购数量"
					})
				}
				this.$refs.popup.open()
			},
			close() {
				this.$refs.popup.close()
			},
			agree() {
				if (!this.ruleChecked) {
					uni.showToast({
						icon:"none",
						title:"请阅读并同意《公司服务协议》"
					})
				} else {
					if (!uni.getStorageSync("userInfo").is_checked) {
						uni.showModal({
							content: "需实名认证",
							confirmText: "去认证",
							cancelText: "取消",
							confirmColor: "#57BE6A",
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/center/accountSet/certification/certification?sgback=true"
									})
								} else if (res.cancel) {}
								
							}
						})
						return
					}
					if (!uni.getStorageSync("userInfo").is_checked) {
						uni.showModal({
							content: "需实名认证",
							confirmText: "去认证",
							cancelText: "取消",
							confirmColor: "#57BE6A",
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/center/accountSet/certification/certification?sgback=true"
									})
								} else if (res.cancel) {}
								
							}
						})
						return
					}
					if (!uni.getStorageSync("userInfo").is_bind_bank) {
						uni.showModal({
							content: "需绑定银行卡",
							confirmText: "去绑定",
							cancelText: "取消",
							confirmColor: "#57BE6A",
							success(res) {
								if (res.confirm) {
									uni.redirectTo({
										url: "/pages/center/accountSet/bank/addBank"
									})
								} else if (res.cancel) {}
								
							}
						})
						
						return
					}
					uni.showLoading({
						mask:true
					})
					shellrecycleApply({
						buyer_id:this.companyInfo.buyer_id,
						shell_id:this.shell.id,
						shell_num: this.number
					}).then(res => {
						uni.hideLoading()
						uni.showToast({
							title:res.msg,
							duration:1000
						})
						setTimeout(function() {
							uni.navigateBack({
							    delta: 2
							});
						},1000)
						
					})
				}
			}
		},
		onHide() {
			this.$refs.popup.close()
		}
	}
</script>

<style scoped lang="scss">
	.container {
		.cut {
			width: 750rpx;
			height: 20rpx;
			background-color: #f5f5f5;
		}

		.company {
			padding: 50rpx 30rpx;
			display: flex;

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 10rpx;
			}

			.info {
				height: 100rpx;
				width: 570rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 20rpx;

				.a {
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
				}

				.b {
					font-size: 24rpx;
					color: #999;
				}
			}
		}

		.mbody {
			padding: 50rpx 30rpx;

			.titlem {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				line-height: 80rpx;
			}

			.titleDteals {
				position: relative;
				border-bottom: 1rpx solid #999;

				.myNumber {
					height: 80rpx;
					line-height: 80rpx;
					font-size: 40rpx;
					font-weight: bold;
					color: #333;
				}

				.qbText {
					position: absolute;
					right: 0;
					top: 0;
					height: 80rpx;
					line-height: 80rpx;
					padding-left: 20rpx;
					font-size: 28rpx;
					color: #666;
					z-index: 2;
				}
			}

			.notice {
				font-size: 20rpx;
				line-height: 50rpx;
				color: #999;
			}
		}

		.btn {
			position: fixed;
			left: 30rpx;
			background-color: $theme-color;
			bottom: 80rpx;
			bottom: calc(80rpx + env(safe-area-inset-bottom));
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: rgba(255, 255, 255, 1);
			border-radius: 40rpx;
		}
	}
	
	.ruleBox{
		height: 80vh;
		background: #fff;
		padding: 50rpx 30rpx 10rpx;
		box-sizing: border-box;
		border-radius: 20rpx 20rpx 0 0;
		.close {
			position: absolute;
			right: 20rpx;
			top: 20rpx;
			width: 24rpx;
			height: 24rpx;
		}
		.content{
			height: calc(80vh - 310rpx);
			height: calc(80vh - 310rpx - env(safe-area-inset-bottom));
			overflow-y: scroll;
		}
		.btnBox {
			position: absolute;
			left: 0;
			bottom: 0;
			padding-bottom: env(safe-area-inset-bottom);
			width: 100%;
			height: 260rpx;
			background-color: #fff;
		
			.rule {
				margin-top: 20rpx;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
		
				image {
					margin-right: 14rpx;
					width: 40rpx;
					height: 40rpx;
					border-radius: 20rpx;
				}
		
				.gray {
					margin-right: 14rpx;
					box-sizing: border-box;
					border: 2rpx #999 solid;
					width: 40rpx;
					height: 40rpx;
					border-radius: 20rpx;
				}
		
				.text {
					font-size: 24rpx;
					color: #333;
		
					text:nth-of-type(2) {
						color: $theme-color;
					}
				}
			}
		
			.btn {
				margin: 30rpx auto 0;
				width: 690rpx;
				height: 80rpx;
				border-radius: 40rpx;
				text-align: center;
				line-height: 80rpx;
				background-color: $theme-color;
				font-size: 30rpx;
				color: #fff;
			}
		}
	}
</style>
